//
//  HeadLineDataHandle.m
//  YouWen
//
//  Created by liqiang－pc on 15/3/10.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "HeadLineDataHandle.h"

@implementation HeadLineDataHandle
+(id)shareHeadLineDataHandle
{
    static HeadLineDataHandle *data = nil;
    if (data == nil) {
        data = [[HeadLineDataHandle alloc] init];
    }
    return data;
}



static sqlite3 *headDB;

- (void)openDB
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFilePath = [array lastObject];
    NSLog(@"%@", documentFilePath);
    NSString *sqlitePath = [documentFilePath stringByAppendingPathComponent:@"headLine.sqlite"];
    NSLog(@"sssssssssssss%@", sqlitePath);
    
    if (headDB != nil) {
        return;
    }
    
    int result = sqlite3_open(sqlitePath.UTF8String, &headDB);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
}


//创建数据库表
- (void)createTable
{
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS headLine(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, thumbnailUrl TEXT, date TEXT, songtime TEXT, ImageUrl TEXT,  contents TEXT, laizhi TEXT)";
    
    //执行sql语句
    int result = sqlite3_exec(headDB, createSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建表失败");
    }
    
}


//插入数据
- (void)insertHeadLine:(HeadLine *)HeadLine;
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO headLine(title, thumbnailUrl,  date, songtime, ImageUrl, contents, laizhi) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@')", HeadLine.title,HeadLine.thumbnailUrl,HeadLine.date,HeadLine.songtime,HeadLine.ImageUrl,HeadLine.contents,HeadLine.laizhi];
    int reslute = sqlite3_exec(headDB, insertSql.UTF8String, NULL, NULL, nil);
    if (reslute == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
    
}

//删除一个数据
- (void)deleteHeadLine:(HeadLine *)headLineModel;
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM headLine WHERE title = '%@'", headLineModel.title];
    
    int result = sqlite3_exec(headDB, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}


//查询所有数据
- (NSMutableArray *)selectHead
{
    NSString *selectSql = @"SELECT * FROM headLine";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(headDB, selectSql.UTF8String, -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        //开始循环遍历查询数据库每一行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //当跟随指针指向那一行的数据符合查询条件,就在while里返回
            //逐列取出每一列的数据
            
            
//            int number = sqlite3_column_int(stmt, 0);
            //column是列的意思      参数的意思是遍历第0列  类型为int型
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *thumbnailUrl = sqlite3_column_text(stmt, 2);
            const unsigned char *date = sqlite3_column_text(stmt, 3);
            const unsigned char *songtime = sqlite3_column_text(stmt, 4);
            const unsigned char *ImageUrl = sqlite3_column_text(stmt, 5);
            const unsigned char *contents = sqlite3_column_text(stmt, 6);
            const unsigned char *laizhi = sqlite3_column_text(stmt, 7);
            
            

            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            
            NSString *thumbnailUrlStr = [NSString stringWithUTF8String:(const char *)thumbnailUrl];
             NSString *dateStr = [NSString stringWithUTF8String:(const char *)date];
            NSString *songtimeStr = [NSString stringWithUTF8String:(const char *)songtime];
            NSString *ImageUrlStr = [NSString stringWithUTF8String:(const char *)ImageUrl];
            
            NSString *contentsStr = [NSString stringWithUTF8String:(const char *)contents];
            NSString *laizhiStr = [NSString stringWithUTF8String:(const char *)laizhi];
           
            
            
            
            
            HeadLine *headLine = [[HeadLine alloc] init];
            
            headLine.title = titleStr;
            headLine.thumbnailUrl = thumbnailUrlStr;
            headLine.date = dateStr;
            headLine.songtime = songtimeStr;
            headLine.ImageUrl = ImageUrlStr;
            headLine.contents = contentsStr;
            headLine.laizhi = laizhiStr;
            
            
            
            //将学生对象存入大数组中
            [array addObject:headLine];
        }
        
    }else{
        NSLog(@"查询准备失败");
    }
    
    return array;
    
}

#pragma mark --清空数据库表
-(void)DropTable
{
    
    NSString *dropSql = @"DROP TABLE lanou25";
    int result =  sqlite3_exec(headDB, dropSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"清除表成功");
        headDB = nil;
    }else
    {
        
        NSLog(@"清除表失败");
    }
    
}

@end
